package plugins.AI;

import kd.bos.bill.BillShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.cache.CacheFactory;
import kd.bos.cache.DistributeSessionlessCache;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.ShowType;
import kd.bos.form.control.Button;
import kd.bos.form.control.Control;
import kd.bos.form.control.Image;
import kd.bos.form.control.Label;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.url.UrlService;
import kd.sdk.plugin.Plugin;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;

/**
 * 动态表单插件
 */
public class MiniShelfRender extends AbstractFormPlugin implements Plugin {
    @Override
    public void registerListener(EventObject event) {
        super.registerListener(event);
        // 获取分布式无会话缓存
        DistributeSessionlessCache cache = CacheFactory.getCommonCacheFactory().getDistributeSessionlessCache("customRegion");

        // 为每个按钮添加点击监听器
        Button button1 = this.getView().getControl("dcb4_imageap1");
        Label label1 = this.getView().getControl("dcb4_labelap1");
        label1.addClickListener(this);
        button1.addClickListener(this);
        Button button2 = this.getView().getControl("dcb4_imageap2");
        Label label2 = this.getView().getControl("dcb4_labelap2");
        label2.addClickListener(this);
        button2.addClickListener(this);
        Button button3 = this.getView().getControl("dcb4_imageap3");
        Label label3 = this.getView().getControl("dcb4_labelap3");
        label3.addClickListener(this);
        button3.addClickListener(this);
        Button button4 = this.getView().getControl("dcb4_imageap4");
        Label label4 = this.getView().getControl("dcb4_labelap4");
        label4.addClickListener(this);
        button4.addClickListener(this);
        Button button5 = this.getView().getControl("dcb4_imageap5");
        Label label5 = this.getView().getControl("dcb4_labelap5");
        label5.addClickListener(this);
        button5.addClickListener(this);
        Button button6 = this.getView().getControl("dcb4_imageap6");
        Label label6 = this.getView().getControl("dcb4_labelap6");
        label6.addClickListener(this);
        button6.addClickListener(this);
        Button button7 = this.getView().getControl("dcb4_imageap7");
        Label label7 = this.getView().getControl("dcb4_labelap7");
        label7.addClickListener(this);
        button7.addClickListener(this);
        Button button8 = this.getView().getControl("dcb4_imageap8");
        Label label8 = this.getView().getControl("dcb4_labelap8");
        label8.addClickListener(this);
        button8.addClickListener(this);
        Button button9 = this.getView().getControl("dcb4_imageap9");
        Label label9 = this.getView().getControl("dcb4_labelap9");
        label9.addClickListener(this);
        button9.addClickListener(this);
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);

        // 获取点击的控件
        Control source = (Control) evt.getSource();
        // 获取分布式无会话缓存
        DistributeSessionlessCache cache = CacheFactory.getCommonCacheFactory().getDistributeSessionlessCache("customRegion");

        String fields = "name";
        QFilter[] filters = new QFilter[0];

        // 加载菜品列表
        DynamicObject[] dys = BusinessDataServiceHelper.load("dcb4_dishes", fields, filters);

        // 从缓存中获取菜名
        int index = Integer.parseInt(source.getKey().substring(12));
        String dishesName = cache.get("dishesSearch" + source.getKey().substring(12));
        String dishesNameTrimmed = dishesName.replaceAll("\\s+", "");
        QFilter qFilter = new QFilter("name", QCP.equals, dishesNameTrimmed);

        DynamicObject single = null;
        // 查找匹配的书籍
        for (DynamicObject result : dys) {
            if (result.getString("name").replaceAll("\\s+", "").equals(dishesNameTrimmed)) {
                single = result;
                BillShowParameter billShowParameter = new BillShowParameter();
                billShowParameter.setFormId("dcb4_dishes");
                billShowParameter.getOpenStyle().setShowType(ShowType.Modal);
                billShowParameter.setStatus(OperationStatus.VIEW);
                Long pkId = (Long) single.getPkValue();
                billShowParameter.setPkId(pkId);
                // 显示书籍详情表单
                this.getView().showForm(billShowParameter);
                break;
            }
        }
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);

        // 获取分布式无会话缓存
        DistributeSessionlessCache cache = CacheFactory.getCommonCacheFactory().getDistributeSessionlessCache("customRegion");
        int cnt = Integer.parseInt(cache.get("dishesSearchCnt"));
        if (cnt >= 9) {
            cnt = 9;
        }
        // 为每个按钮添加点击监听器，并根据缓存中的书籍数量设置按钮的可见性
        for (int i = 1; i <= 9; i++) {
            this.addClickListeners("dcb4_image" + i);
            if (i > cnt) {
                this.getView().setVisible(false, "dcb4_image" + i);
                // this.getView().setVisible(true, "dcb4_imageap" + i);
            }
        }
        List<String> dishesArray = new ArrayList<>();
        for (int i = 1; i <= cnt; i++) {
            dishesArray.add(cache.get("dishesSearch" + i));
        }

        String fields = "name,dcb4_picture";
        QFilter[] filters = new QFilter[0];
        // 加载书籍列表
        DynamicObject[] dys = BusinessDataServiceHelper.load("dcb4_dishes", fields, filters);
        int index = 0;
        // 设置每个按钮对应的图片
        for (String v : dishesArray) {
            index++;
            for (DynamicObject single : dys) {
                if (single.getString("name").replaceAll("\\s+", "").equals(v.replaceAll("\\s+", ""))) {
                    String url = single.getString("dcb4_picture");
                    String name = single.getString("name");
                    String imageFullUrl = UrlService.getAttachmentFullUrl(url);
                    Image image = this.getView().getControl("dcb4_imageap" + index);
                    Label label = this.getView().getControl("dcb4_labelap" + index);
                    image.setUrl(imageFullUrl);
                    label.setText(name);
                    break;
                }
            }
        }
    }
}
